package com.example.demo.controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.entity.AlterDemand;
import com.example.demo.entity.QueryInfo;
import com.example.demo.entity.UpdateCheck;
import com.example.demo.service.AlterDemandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;



@RestController
@Slf4j
@RequestMapping(value = "/AlterDemand")
@Api(value = "需求新增更改接口", tags = "需求新增更改相关的接口" )
public class AlterDemandController {

    @Autowired
    AlterDemandService alterDemandService;
    @RequestMapping(value = "/addDemand",method = RequestMethod.POST)
    @ApiOperation(value = "添加需求", notes = "添加需求")
    //  所需参数可以对照文档即可
    public String addDemand(AlterDemand alterDemand) {
        log.info("开始添加新需求...{}",alterDemand);
        int i = alterDemandService.addDemand(alterDemand);
        String str = i >0? "success":"error";
        return str;
    }

    @RequestMapping(value = "/alterDemand",method = RequestMethod.POST)
    @ApiOperation(value = "修改需求", notes = "修改需求")
    //  所需参数对照文档即可
    public String AlterDemand(AlterDemand alterDemand){
        System.out.println(alterDemand);
        int i = alterDemandService.AlterDemand(alterDemand);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping(value = "/updateCheck", method = RequestMethod.PUT)
    @ApiOperation(value = "审核需求", notes = "审核需求")
    //  所需参数对照entity中的UpdateCheck
    public String updateCheck(UpdateCheck updateCheck) {
        System.out.println(updateCheck);
        int i = alterDemandService.updateCheck(updateCheck);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping(value = "/getAlterDemand", method = RequestMethod.GET)
    @ApiOperation(value = "查询需修改需求",notes = "查询需修改需求")
    //  所需参数对照entity中的QueryInfo
    public String getAlterDemandList(QueryInfo queryInfo){
        int numbers = alterDemandService.getAlterDemandCounts(queryInfo.getBgProject(),queryInfo.getQuery(),queryInfo.getIf_check(), queryInfo.getTime_classification());// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<AlterDemand> alterdemands = alterDemandService.getAllAlterDemand(queryInfo.getBgProject(),queryInfo.getQuery(),queryInfo.getIf_check(),
                queryInfo.getTime_classification(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("number",numbers);
        res.put("data",alterdemands);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println(users_json);
        return users_json;
    }
}
